package com.tensquare.sms.listener;
/*
 *ClassName:SmsListener
 *Description:短信监听类，监听队列中存放的验证码
 *@author:可爱的大鸭子
 */

import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.tensquare.sms.utils.SmsUtil;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
@RabbitListener(queues = "sms")
public class SmsListener {

    @Autowired
    private SmsUtil smsUtil;
    @Value("${aliyun.sms.template_code}")
    private String template_code;//模板编号
    @Value("${aliyun.sms.sign_name}")
    private String sign_name;//签名

    /**
     * 发送短信
     * @param message
     */
    @RabbitHandler
    public void sendSms(Map<String,String> message){
        System.out.println("手机号："+message.get("mobile"));
        System.out.println("验证码："+message.get("code"));
        try {
            System.out.println(template_code);
            System.out.println(sign_name);
            System.out.println();
            SendSmsResponse sendSmsResponse = smsUtil.sendSms(message.get("mobile"), template_code, sign_name,
                    "{\"number\":\""+message.get("mobile")+"\",\"code\":\""+message.get("code")+"\"}");
            System.out.println(sendSmsResponse.getMessage());
        } catch (ClientException e) {
                e.printStackTrace();
        }
    }
}